package cn.yzgy.yzgyoa.ucenter.mapper;

import cn.yzgy.yzgyoa.ucenter.dto.RoleDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysRole;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 用户角色表 Mapper 接口
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-20
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

    /**
     * 根据用户UID获取用户角色列表
     * @param uid uid
     * @return 用户角色列表
     */
    List<SysRole> getRolesByUid(String uid);

    /**
     * 查询角色相关数据
     * @param num 从第num条数据开始查询
     * @param size 查询size条数据
     * @return map集合
     */
    @Select("SELECT a.role_id as roleId,a.is_sys as isSys,a.role_name as roleName ,a.role_remarks as roleRemarks," +
            " a.state as state, GROUP_CONCAT(c.p_val) as permissionId ,GROUP_CONCAT(c.p_name) as permissionName " +
            "FROM sys_role a,sys_role_permission b,sys_permission c " +
            "where a.role_id=b.r_id and b.p_id=c.p_id and a.role_id <> '0' group by a.role_id,a.is_sys,a.role_name,a.role_remarks,a.state LIMIT ${num},${size}")
    List<RoleDTO> queryAll(@Param("num")Integer num, @Param("size")Integer size);

    /**
     * 展示数据总条数
     * @return 展示数据总条数
     */
    @Select("select count(1) from (SELECT a.role_id as roleId,a.is_sys as isSys,a.role_name as roleName ,a.role_remarks as roleRemarks," +
            " a.state as state, GROUP_CONCAT(c.p_val) as permissionId ,GROUP_CONCAT(c.p_name) as permissionName " +
            "FROM sys_role a,sys_role_permission b,sys_permission c " +
            "where a.role_id=b.r_id and b.p_id=c.p_id and a.role_id <> '0' group by a.role_id,a.is_sys,a.role_name,a.role_remarks,a.state ) h ")
    int countData();

    /**
     * 获取新增数据主键
     * @return 新增数据主键
     */
    @Select("select max(role_id)+1 as newid from sys_role ")
    int getNewid();

    /**
     * 新增数据查重
     * @param wrapper 查询条件集
     * @return 重复数据
     */
    @Select("select * from sys_role  ${ew.customSqlSegment} ")
    SysRole check(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 所有角色数据列表
     * @return 角色数据列表
     */
    @Select(" select * from sys_role where role_id <> '0'")
    List<SysRole> getSysRoleList();

    /**
     * 停用角色
     * @param wrapper 条件集
     * @return 修改条数
     */
    @Update("update sys_role set state='0' ${ew.customSqlSegment}")
    int stopState(@Param("ew") Wrapper wrapper);

    /**
     * 启用角色
     * @param wrapper 条件集
     * @return 修改条数
     */
    @Update("update sys_role set state='1'${ew.customSqlSegment}")
    int startState(@Param("ew") Wrapper wrapper);
}
